import Vue from 'vue'
import Router from 'vue-router'
import { AUTHS } from '../assets/js/pageAuth.js'
// import SysManage from './sysManage'
import SysManage from '@/view/sysManage/index'
import ModelSetting from '@/view/sysManage/modelSetting'
import Enterprise from '@/view/enterprise/enterPrise'
import KeepGoing from '@/view/errorPage/going'
import earlyWarningSetting from '@/view/sysManage/earlyWarningSetting'
import earlyWarningAlgorithm from '@/view/sysManage/earlyWarningAlgorithm'
import FilesUpload from '@/view/sysManage/filesUpload'
import ListUpload from '@/view/sysManage/listUpload'
import RuleSetting from '@/view/sysManage/ruleSetting'
import KeyWord from '@/view/sysManage/keyWord'
import PersonInformation from '@/view/sysManage/personinformation'
import WarnPush from '@/view/sysManage/warnPush'
import taskInfo from '@/view/sysManage/taskInfo'
import taskList from '@/view/sysManage/taskList'
import taskRun from '@/view/sysManage/taskRun'
import whiteList from '@/view/sysManage/whiteList'
import store from '../store'

const Login = r =>
  require.ensure([], () => r(require('@/view/login/login')), 'Login')
const Layout = r =>
  require.ensure([], () => r(require('@/view/layout')), 'Homepage')
const Homepage = r =>
  require.ensure([], () => r(require('@/view/home/homepage')), 'Homepage')
const notfound = r =>
  require.ensure([], () => r(require('@/view/errorPage/404')), 'Homepage')
const StatAnaly = r =>
  require.ensure([], () => r(require('@/view/statAnaly/statAnaly')), 'Homepage')
// 风险预警
const EntDetail = r =>
  require.ensure([], () => r(require('@/view/entDetail/index')), 'Detail')
const TOPO = r =>
  require.ensure([], () => r(require('@/view/entDetail/topo')), 'topo')
const BaseInfo = r =>
  require.ensure([], () => r(require('@/view/entDetail/baseInfo')), 'Detail')
const Address = r =>
  require.ensure([], () => r(require('@/view/entDetail/address')), 'Detail')
const RiskCompliance = r =>
  require.ensure(
    [],
    () => r(require('@/view/entDetail/riskCompliance')),
    'Detail'
  )
const DetailSpecialWord = r =>
  require.ensure([], () => r(require('@/view/entDetail/specialWord')), 'Detail')
const RiskManagement = r =>
  require.ensure(
    [],
    () => r(require('@/view/entDetail/riskManagement')),
    'Detail'
  )
const RiskRelation = r =>
  require.ensure(
    [],
    () => r(require('@/view/entDetail/riskRelation')),
    'Detail'
  )
const RiskOther = r =>
  require.ensure([], () => r(require('@/view/entDetail/riskOther')), 'Detail')
const riskDiffuse = r =>
  require.ensure([], () => r(require('@/view/entDetail/riskDiffuse')), 'Detail')
const StatusChange = r =>
  require.ensure(
    [],
    () => r(require('@/view/entDetail/statusChange')),
    'Detail'
  )
const Snapshot = r =>
  require.ensure([], () => r(require('@/view/entDetail/snapshot')), 'Detail')
const RiskTable = r =>
  require.ensure(
    [],
    () => r(require('@/view/entDetail/riskTable')),
    'Detail'
  )
// 企业库路由
const Search = r =>
// 企业库查找
  require.ensure([], () => r(require('@/view/clib/search')), 'clib')
const Cliblist = r =>

  require.ensure([], () => r(require('@/view/clib/list')), 'clib')
const ClibDetail = r =>

  require.ensure([], () => r(require('@/view/clib/detail-index')), 'clib')
const ClibDetailbase = r =>
// 基本信息
  require.ensure([], () => r(require('@/view/clib/baseInfo')), 'clib')

const ClibProperty = r =>
// 无形财产
  require.ensure([], () => r(require('@/view/clib/inProperty')), 'clib')
const ClibState = r =>
// 经营状况
  require.ensure([], () => r(require('@/view/clib/operationState')), 'clib')
const ClibInformation = r =>
// 风险信息
  require.ensure([], () => r(require('@/view/clib/riskInformation')), 'clib')
const ClibReports = r =>
// 企业年报
  require.ensure([], () => r(require('@/view/clib/annualReports')), 'clib')
const ClibOpinion = r =>
// 企业舆情
  require.ensure([], () => r(require('@/view/clib/publicOpinion')), 'clib')
const ClibSnapshot = r =>
  require.ensure([], () => r(require('@/view/clib/snapshot')), 'clib')

const RelationDetail = r =>
  require.ensure(
    [],
    () => r(require('@/view/relationDetail/index')),
    'relationDetail'
  )
const Relationlbase = r =>
  require.ensure(
    [],
    () => r(require('@/view/relationDetail/baseInfo')),
    'relationDetail'
  )
const RelationCompliance = r =>
  require.ensure(
    [],
    () => r(require('@/view/relationDetail/riskCompliance')),
    'relationDetail'
  )
const RelationManage = r =>
  require.ensure(
    [],
    () => r(require('@/view/relationDetail/riskManagement')),
    'relationDetail'
  )
const Relationspecial = r =>
  require.ensure(
    [],
    () => r(require('@/view/relationDetail/specialWord')),
    'relationDetail'
  )
// 企业黑名单库路由
const BlacklistDetail = r =>
  require.ensure(
    [],
    () => r(require('@/view/blacklist')),
    'blacklist'
  )
const Blacklist = r =>
  require.ensure(
    [],
    () => r(require('@/view/blacklist/blacklist')),
    'blacklist'
  )
const Analysis = r =>
  require.ensure(
    [],
    () => r(require('@/view/blacklist/analysis')),
    'blacklist'
  )
// 总览
const Pandect = r =>
  require.ensure([], () => r(require('@/view/pandect/pandect')), 'pandect')
// 处置管理系统
const Managements = r => require.ensure([], () => r(require('@/view/managements/index')), 'managements')
const ManageList = r => require.ensure([], () => r(require('@/view/managements/manageList')), 'managements')
const ManageState = r => require.ensure([], () => r(require('@/view/managements/manageState')), 'managements')
const History = r => require.ensure([], () => r(require('@/view/managements/history')), 'managements')

// 路由是否已经动态加载
// var appendRouter = false

Vue.use(Router)
// 装载静态路由，不需要权限验证
let router = new Router({
  // mode: 'history',
  routes: [
    {
      name: 'login',
      path: '/login',
      component: Login,
      meta: {
        requireAuth: false // 添加该字段，表示进入这个路由是不需要登录的
      }
    },
    {
      name: 'notfound',
      path: '/404',
      component: notfound,
      meta: {
        requireAuth: false // 添加该字段，表示进入这个路由是不需要登录的
      }
    },
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: 'homepage',
          component: Homepage,
          meta: {
            auth: AUTHS.Home
          }
        },
        // 查看快照
        {
          path: 'statAnaly',
          name: 'statAnaly',
          component: StatAnaly
        },
        {
          path: 'snapshot',
          name: 'snapshot',
          component: Snapshot
        },
        // 图谱全屏展示
        {
          path: 'topo',
          name: 'topo',
          component: TOPO
        },
        {
          path: 'snapshot1',
          name: 'snapshot1',
          component: ClibSnapshot
        },
        // 企业监管
        {
          path: 'enterPrise',
          component: Enterprise
        },
        // 关联图谱进入的详情
        {
          path: 'relationDetail',
          name: 'relationDetail',
          component: RelationDetail,
          children: [
            // 基本信息路由
            {
              path: 'baseinfo2',
              name: 'baseinfo2',
              component: Relationlbase
            },
            // 合规风险
            {
              path: 'riskcompliance2',
              name: 'riskcompliance2',
              component: RelationCompliance
            },
            // 经营风险
            {
              path: 'riskmanagement2',
              name: 'riskmanagement2',
              component: RelationManage
            },
            // 舆情风险
            {
              path: 'specialword2',
              name: 'specialword2',
              component: Relationspecial
            }
            // {
            //   path: '',
            //   redirect: 'baseinfo2'
            // }
          ]
        },
        // 企业库
        {
          path: 'clibsearch',
          name: 'clibsearch',
          component: Search
        },
        {
          path: 'enterPrise1',
          name: 'enterPrise1',
          component: Cliblist
        },
        {
          path: 'clibdetail',
          name: 'clibdetail',
          component: ClibDetail,
          children: [
            {
              path: 'baseinfoa',
              name: 'baseinfoa',
              component: ClibDetailbase
            },
            // 无形财产
            {
              path: 'InProperty',
              name: 'InProperty',
              component: ClibProperty
            },
            // 经营状况
            {
              path: 'operationState',
              name: 'operationState',
              component: ClibState
            },
            // 风险信息
            {
              path: 'riskInformations',
              name: 'riskInformations',
              component: ClibInformation
            },
            // 企业年报
            {
              path: 'annualReports',
              name: 'annualReports',
              component: ClibReports
            },
            // 企业舆情
            {
              path: 'publicOpinion',
              name: 'publicOpinion',
              component: ClibOpinion
            }
          ]
        },
        // 详情
        {
          path: 'entdetail',
          name: 'entdetail',
          component: EntDetail,
          // meta: {
          //   auth: AUTHS.Home
          // },
          children: [
            // 基本信息路由
            {
              path: 'baseinfo',
              name: 'baseinfo',
              component: BaseInfo
            },
            // 企业地址信息路由
            {
              path: 'address',
              name: 'address',
              component: Address
            },
            // 合规风险
            {
              path: 'riskcompliance',
              name: 'riskcompliance',
              component: RiskCompliance
            },
            // 经营风险
            {
              path: 'riskmanagement',
              name: 'riskmanagement',
              component: RiskManagement
            },
            // 舆情风险
            // {
            //   path: 'risksentiment',
            //   name: 'risksentiment',
            //   component: RiskSentiment
            // },
            // 舆情风险
            {
              path: 'specialword',
              name: 'specialword',
              component: DetailSpecialWord
            },
            // 关联风险
            {
              path: 'riskrelation',
              name: 'riskrelation',
              component: RiskRelation
            },
            // 收益率风险
            {
              path: 'riskother',
              name: 'riskother',
              component: RiskOther
            },
            // 传播风险
            {
              path: 'riskdiffuse',
              name: 'riskdiffuse',
              component: riskDiffuse
            },
            // 操作记录
            {
              path: 'statuschange',
              name: 'statuschange',
              component: StatusChange
            },
            // 风险总表
            {
              path: 'risktable',
              name: 'risktable',
              component: RiskTable
            }
          ]
        },
        // SysManage
        {
          path: 'sysManage',
          component: SysManage,
          children: [
            {
              path: '',
              // redirect: '/sysManage/modelSetting'
              redirect: '/sysManage/keyWord'
            },
            {
              path: 'modelSetting',
              component: ModelSetting
            },
            {
              path: 'earlyWarningAlgorithm',
              component: earlyWarningAlgorithm
            },
            {
              path: 'modelSetting/earlyWarningSetting',
              component: earlyWarningSetting
            },
            // 后期新增文件、列表上传页
            {
              path: 'filesUpload',
              component: FilesUpload
            },
            {
              path: 'listUpload',
              component: ListUpload
            },
            {
              name: 'keepGoing',
              path: 'keepGoing',
              component: KeepGoing
            },
            // 烽火值计算
            {
              name: 'ruleSetting',
              path: 'ruleSetting',
              component: RuleSetting
            },
            // 烽火值同步计算
            {
              path: 'taskInfo',
              component: taskInfo,
              children: [
                {
                  path: '',
                  redirect: taskList
                },
                {
                  path: 'taskList',
                  name: 'taskList',
                  component: taskList
                },
                {
                  path: 'taskRun',
                  name: 'taskRun',
                  component: taskRun
                }
              ]
            },
            {
              path: 'whiteList',
              name: 'whiteList',
              component: whiteList
            },
            // 关键词设置
            {
              name: 'keyWord',
              path: 'keyWord',
              component: KeyWord
            },
            // 个人信息
            {
              name: 'personinformation',
              path: 'personinformation',
              component: PersonInformation
            },
            // 预警推送
            {
              name: 'warnPush',
              path: 'warnPush',
              component: WarnPush
            }
          ]
        },
        // 企业黑名单库
        {
          path: '/blacklistDetail',
          component: BlacklistDetail,
          children: [
            {
              path: '',
              redirect: 'analysis'
            },
            {
              path: 'blacklist',
              name: 'blacklist',
              component: Blacklist
            },
            {
              path: 'analysis',
              name: 'analysis',
              component: Analysis
            }
          ]
        },
        // 处置管理系统
        {
          path: '/managements',
          component: Managements
        },
        // 处置反馈列表
        {
          path: '/ManageList',
          // name: 'ManageList',
          component: ManageList,
          children: [
            {
              path:'',
              redirect: 'manageState'
            },
            {
              path: 'manageState',
              name: 'manageState',
              component: ManageState
            },
            {
              path: 'history',
              name: 'history',
              component: History
            }
          ]
        },
        // 总览
        {
          path: '/pandect',
          name: 'pandect',
          component: Pandect
        },
        {
          path: '',
          redirect: 'homepage'
        }
      ]
    },
    {
      path: '**',
      redirect: '/404',
      meta: {
        requireAuth: false
      }
    }
  ],
  linkActiveClass: 'selected'
})
// router.push(notfoundRouter)
// router.push(asyncRouterMap)

store.dispatch('refresh')
// appendAsyncRoutes(router)

router.beforeEach((to, from, next) => {
  let isLogin = store.getters.isLogin
  if (isLogin) {
    next()
  } else {
    if (to.name === 'login' && !isLogin) {
      next()
    } else {
      next({ name: 'login' })
    }
    // next()
  }
})

/* router.beforeEach((to, from, next) => {
  // 支持单点登录

  // 验证是否登录
  let isLogin = to.meta.requireAuth === false || store.getters.isLogin
  // 验证是否有权限
  if (isLogin) {
    // 装载有权限的路由
    appendAsyncRoutes(router)
    var auth = true
    if (to.meta.auth) {
      var authKey = to.meta.auth
      if (Object.prototype.toString.apply(authKey) === '[object Function]') {
        authKey = authKey(to)
      }
      auth = hasAuth(authKey)
    }
    if (auth) {
      next()
    }
  } else {
    next({
      path: '/login',
      query: {
        r: to.fullPath
      }
    })
  }
}) */

/**
 * 通过权限判断，获取有权限的路由集合
 */
/* function getAuthRoutes(routes) {
  let routerMap = []
  for (var i = 0; i < routes.length; i++) {
    let route = getRoute(routes[i])
    if (route) {
      routerMap.push(route)
    }
  }
  if (routerMap.length) {
    routerMap.push({
      path: '',
      redirect: routerMap[0].path
    })
  }
  return routerMap

  // 构造路由对象
  function buildRoute(route) {
    return {
      name: route.name,
      path: route.path,
      component: route.component,
      meta: route.meta
    }
  }
  // 路由权限验证
  function getRoute(route) {
    var newRoute = null
    let auth = true
    // 判断是否有权限访问路由
    if (route.meta && route.meta.auth) {
      auth = hasAuth(route.meta.auth)
    }
    if (auth) {
      newRoute = buildRoute(route)
      if (route.children) {
        // 如果有子路由，循环调用获取有权限的路由
        let children = getAuthRoutes(route.children)
        if (children && children.length) {
          newRoute.children = children
        }
      }
    }
    return newRoute
  }
} */

/* function appendAsyncRoutes(router) {
  if (!appendRouter && store.getters.isLogin) {
    var routes = getAuthRoutes(asyncRouterMap)
    // 装载 404 路由，注意一定要放在最后加载
    routes.push(notfoundRouter)
    router.addRoutes(routes)
    appendRouter = true
  }
} */
export default router



// WEBPACK FOOTER //
// ./src/router/index.js